package com.chookss.mine.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.entity.UploadEntity;
import com.chookss.home.studyPlan.StudyPlanAddActivity;
import com.chookss.video.recommend.VedioDetailActivity;
import com.google.gson.reflect.TypeToken;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.home.publish.PublishVedioActivity;
import com.chookss.mine.entity.MyViedeoEntity;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyEvent;
import com.chookss.tools.Utils;
import com.chookss.video.VideoOnePlayActivity;
import com.chookss.video.VideoSetsPlayActivity;
import com.chookss.video.entity.VideoEntity;
import com.johnrambo.ktea.common.klogutil.KLog;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.annotations.Nullable;

public class MyVideoAdapter extends BaseQuickAdapter<MyViedeoEntity.VideoListBean, BaseViewHolderEx> {
    private Context context;
    private int width, height;
    private String vedioType = "2";//1打开单个2跳转集合
    private int employeeCodeType;//0自己的主页1别人的主页

    public MyVideoAdapter(int layoutResId, Context context, @Nullable List<MyViedeoEntity.VideoListBean> data, String vedioType, int employeeCodeType) {
        super(layoutResId, data);
        this.context = context;
        this.employeeCodeType = employeeCodeType;
        this.vedioType = vedioType;
        width = (Utils.getScreenWidthPixel(context) - Utils.dip2px(context, 12)) / 3;
        height = width * 290 / 232;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, MyViedeoEntity.VideoListBean item) {
        RelativeLayout rl = helper.getView(R.id.item_rl);
        RelativeLayout rlPostStatus = helper.getView(R.id.rlPostStatus);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rl.getLayoutParams();
        params.height = height;
        params.width = width;
        rl.setLayoutParams(params);
        LinearLayout llZan = helper.getView(R.id.llZan);
        ImageView ivBg = helper.getView(R.id.ivBg);
        ImageView ivZan = helper.getView(R.id.ivZan);
        ImageView ivStatus = helper.getView(R.id.ivStatus);
        TextView tvTitle = helper.getView(R.id.tvTitle);
        TextView tvZan = helper.getView(R.id.tvZan);
        tvTitle.setText(item.getVideoTitle());
        if("0".equals(item.getPostStatus())){
            //发布中
            llZan.setVisibility(View.GONE);
            rlPostStatus.setVisibility(View.VISIBLE);
            Glide.with(context)
                    .load(Uri.fromFile(new File(item.getVideoCoverPath())))
                    .into(ivBg);
            GlideUtils.load(context, item.getVideoCoverPath(), ivBg, R.drawable.default_vedios);
        }else {
            llZan.setVisibility(View.VISIBLE);
            rlPostStatus.setVisibility(View.GONE);
            if ("0".equals(item.getIsLike())) {
                ivZan.setBackgroundResource(R.drawable.icon_like);
            } else {
                ivZan.setBackgroundResource(R.drawable.icon_liked);
            }
            tvZan.setText(item.getVideoLikeCounts() + "");
            GlideUtils.load(context, item.getVideoCoverPath(), ivBg, R.drawable.default_vedios);
            if ("0".equals(item.getVideoOpenStatus())) {
                //是否公开 0公开1私密
                if ("0".equals(item.getVideoAuditStatus())) {
                    // 0审核中1审核通过2审核未通过
                    ivStatus.setVisibility(View.VISIBLE);
                    ivStatus.setBackgroundResource(R.drawable.icon_shen);
                    item.setIsShare("0");
                } else if ("1".equals(item.getVideoAuditStatus())) {
                    if ("1".equals(item.getVideoStatus())) {
                        //是否上架 0下架1上架
                        ivStatus.setVisibility(View.GONE);
                        item.setIsShare("1");
                    } else {
                        item.setIsShare("0");
                        ivStatus.setVisibility(View.VISIBLE);
                        ivStatus.setBackgroundResource(R.drawable.icon_xiajia);
                    }
                } else {
                    item.setIsShare("0");
                    ivStatus.setVisibility(View.VISIBLE);
                    ivStatus.setBackgroundResource(R.drawable.icon_tui);
                }
            } else {
                item.setIsShare("0");
                ivStatus.setVisibility(View.VISIBLE);
                ivStatus.setBackgroundResource(R.drawable.icon_private);
            }
        }

        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("0".equals(item.getPostStatus())){
                     //发布中
                    MyToast.show("视频正在发布中，请稍后刷新");
                    return;
                }

                VideoEntity video = Utils.modelA2B(item);
                if (video == null) {
                    return;
                }
                Intent intent = null;
                KLog.i(item.getVideoId() + "==");
                if ("1".equals(vedioType)) {
                    intent = new Intent(context, VideoOnePlayActivity.class);

                    intent.putExtra("watermark", "2");
                    intent.putExtra("video", video);
                } else {
                    if(Utils.isNull(item.getVideoCollectionsCode())){
                        intent = new Intent(context, VideoOnePlayActivity.class);

                        intent.putExtra("watermark", "2");
                        intent.putExtra("video", video);
                    }else{
                        intent = new Intent(context, VideoSetsPlayActivity.class);
                        intent.putExtra("video", video);
                        intent.putExtra("watermark", "4");
                        if (0 == employeeCodeType) {
                            intent.putExtra("type", "3");
                        } else {
                            intent.putExtra("type", "1");
                        }
                    }


                }
                context.startActivity(intent);
            }
        });
        helper.getItemView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if ("0".equals(item.getVideoOpenStatus())) {
                    //是否公开 0公开1私密
                    if ("2".equals(item.getVideoAuditStatus())) {
                        //是否上架 0审核中1审核通过2审核未通过
                        deleteDialog(item.getId(), item.getVideoId(), helper.getItemPosition(), helper);
                    } else if ("1".equals(item.getVideoAuditStatus())) {
                        if ("0".equals(item.getVideoStatus())) {
                            //是否上架 0下架1上架
                            deleteDialog(item.getId(), item.getVideoId(), helper.getItemPosition(), helper);
                        }else{
                            if("1".equals(item.isSingle)){
                                addVideo(item);
                            }else {
                                if(Utils.isNull(item.getVideoCollectionsCode())){
                                    addVideo(item);
                                }else{
                                    addDialog(item, helper.getItemPosition(), helper);
                                }
                            }

                        }
                    }
                } else {
                    deleteDialog(item.getId(), item.getVideoId(), helper.getItemPosition(), helper);
                }
                return true;
            }
        });
    }

    private int index = -1;
    private void deleteDialog(String Id, String videoId, int position, BaseViewHolder helper) {
        final String[] array = new String[]{"编辑视频", "删除视频"};
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setSingleChoiceItems(array, -1,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        index = which;
                    }
                });
        builder.setPositiveButton("确定", null);
        builder.setNegativeButton("取消", null);
        AlertDialog dialog = builder.create();
        dialog.show();
        dialog.setCanceledOnTouchOutside(true);// dialog弹出后，点击界面其他部分dialog消失
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getActivity(),"保存",Toast.LENGTH_SHORT).show();
                if (index == -1) {
                    MyToast.show("选择视频修改类型");
                    return;
                } else if (index == 0) {
                    index = -1;
                    dialog.dismiss();

                    UploadEntity.VideoBean video = Utils.modelA2BUploadVideo(getData().get(position));
                    Intent intent=new Intent(context, PublishVedioActivity.class);
                    intent.putExtra("data",video);
                    intent.putExtra("type",1);
                    context.startActivity(intent);
                } else if (index == 1) {
                    index = -1;
                    deleteVedio(Id, videoId, position, helper);
                    dialog.dismiss();
                }
            }
        });
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index = -1;
                dialog.dismiss();
            }
        });
    }
    private void addVideo(MyViedeoEntity.VideoListBean videoEntity){
        new android.app.AlertDialog.Builder(context)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Intent intent = new Intent(context, StudyPlanAddActivity.class);
                        intent.putExtra("title", videoEntity.getVideoTitle());
                        intent.putExtra("describe", videoEntity.getVideoKeyWord());
                        intent.putExtra("resourceId", videoEntity.getVideoId());
                        intent.putExtra("time", videoEntity.getVideoSeconds());
                        intent.putExtra("picUrl", videoEntity.getVideoCoverPath());
                        intent.putExtra("resourceType", 1);
                        context.startActivity(intent);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setMessage("确定要视频加入学习任务么?")
                .setTitle("提示")
                .show();

    }
    private int index2 = -1;
    private void addDialog(MyViedeoEntity.VideoListBean videoEntity, int position, BaseViewHolder helper) {
        final String[] array = new String[]{"视频加入学习任务", "视频集合加入学习任务"};
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setSingleChoiceItems(array, -1,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        index2 = which;
                    }
                });
        builder.setPositiveButton("确定", null);
        builder.setNegativeButton("取消", null);
        builder.setTitle("提示");
        AlertDialog dialog = builder.create();
        dialog.show();
        dialog.setCanceledOnTouchOutside(true);// dialog弹出后，点击界面其他部分dialog消失
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getActivity(),"保存",Toast.LENGTH_SHORT).show();
                if (index2 == -1) {
                    MyToast.show("选择加入类型");
                    return;
                } else if (index2 == 0) {
                    index2 = -1;
                    dialog.dismiss();
                    Intent intent = new Intent(context, StudyPlanAddActivity.class);
                    intent.putExtra("title", videoEntity.getVideoTitle());
                    intent.putExtra("describe", videoEntity.getVideoKeyWord());
                    intent.putExtra("resourceId", videoEntity.getVideoId());
                    intent.putExtra("time", videoEntity.getVideoSeconds());
                    intent.putExtra("picUrl", videoEntity.getVideoCoverPath());
                    intent.putExtra("resourceType", 1);
                    context.startActivity(intent);
                } else if (index2 == 1) {
                    index2 = -1;
                    dialog.dismiss();
                    Intent intent = new Intent(context, StudyPlanAddActivity.class);
                    intent.putExtra("title", videoEntity.getVideoCollectionsName());
                    intent.putExtra("resourceId", videoEntity.getVideoCollectionsCode());
                    intent.putExtra("describe", videoEntity.getVideoKeyWord());
                    intent.putExtra("resourceType", 3);
                    context.startActivity(intent);
                }
            }
        });
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index2 = -1;
                dialog.dismiss();
            }
        });
    }
    private void deleteVedio(String Id, String videoId, int position, BaseViewHolder helper) {
        Map<String, String> map = new HashMap<>();
        map.put("id", Id);
        map.put("videoId", videoId);
        MyHttpRequest.getDefault().postRequest(new TypeToken<LzyResponse<String>>() {
        }.getType(), null, Urls.deleteVideoById, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) {
                MyToast.show("删除成功");
                HashMap<String, String> map = new HashMap<>();
                map.put("deleteVideoIdId", videoId);
                EventBus.getDefault().post(new MyEvent(map));
            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
}
